/*
Add_AgencyRecordOwner_PLAS_To_AgencyTeam
Version 1.0
Description: Creates Agency_Team__c records under a Account on insertion of Account record, calls insertAgencyTeam() of
             Account_To_AgencyTeam_PLAS class which performs bussiness logic.
Author: Pratibh Prakash
Created Date: 5 Mar-2009
*/
trigger Add_AgencyRecordOwner_PLAS_To_AgencyTeam on Account (after insert, after update) {

    Set<Id> hrIds = new set<ID>();//Used for getting Hr record having user same as one of four fields CL/PL Territory Manager, CL/PL Agency Specialist.
    map<Id, Agency_Team__c> agtDelete = new map<Id, Agency_Team__c>();//Stores Agency_Team records which are to be deleted when Account record is updated,
    List<Agency_Team__c> existingAGT = new List<Agency_Team__c>();//Stores existing Agency_Team records related to an Account record.
    List<Account> accounts = new List<Account>();
    List<Account> oldAccounts = new List<Account>();
    if(Trigger.isInsert){
        
        for(Integer x=0;x<Trigger.new.size();x++){//Generating HRIds from new insreted records.
            hrIds.add(Trigger.new[x].OwnerId);
            if(Trigger.new[x].PLAS__c != null){
                hrIds.add(Trigger.new[x].PLAS__c);
            }
            if(Trigger.new[x].CL_Agency_Specialist__c != null){
                hrIds.add(Trigger.new[x].CL_Agency_Specialist__c);
            }
            if(Trigger.new[x].PL_Agency_Specialist__c != null){
                hrIds.add(Trigger.new[x].PL_Agency_Specialist__c);
            }
        }
        
        Account_To_AgencyTeam_PLAS.insertAgencyTeam(Trigger.new , hrIds, oldAccounts);
    }
    
    else if(Trigger.isUpdate){
        /*List<String> fields = new List<String>();
		fields.add('RU_PL_Actual_PY__c');
		fields.add('PL_Actual_PY__c');
		fields.add('RU_CL_Actual_PY__c');
		fields.add('CL_Actual_PY__c');
		List<Account> recs = new List<Account>();
		FilterRecords obj = new FilterRecords(Trigger.old, Trigger.new, fields);
		recs = obj.getFilteredRecords_new();
		Set<ID> setAccountId = new Set<ID>();
		setAccountId = obj.getFilterRecordsIDs();
        Agency_Plan__c [] agencyPlanRecords = [select RecordTypeId, RecordType.Name,Account__c from Agency_Plan__c where Plan_Year__c in :arrayYears and Agency__c in:setAccountID];*/
        
        existingAGT = [Select Agency__c, Team_Member_User__c, Team_Member__c, Role__c From Agency_Team__c Where Agency__c In :Trigger.newMap.keySet()];
        
        for(Integer i=0; i<Trigger.new.size(); i++){
            
            //Filtering records which are updated.
            if(Trigger.old[i].OwnerId != Trigger.new[i].OwnerId || Trigger.old[i].PLAS__c != Trigger.new[i].PLAS__c || Trigger.old[i].CL_Agency_Specialist__c != Trigger.new[i].CL_Agency_Specialist__c || Trigger.old[i].PL_Agency_Specialist__c != Trigger.new[i].PL_Agency_Specialist__c ){
                
                for(Agency_Team__c exsAG :existingAGT){ //deletes old agency teams for Territory Manager and PLAS users. 
                    
                    if(Trigger.old[i].OwnerId != Trigger.new[i].OwnerId && Trigger.old[i].OwnerId == exsAG.Team_member_User__c && exsAG.Role__c == 'CL Territory Manager'){
                        agtDelete.put(exsAG.Id, exsAG); //deletes existing agency teams for CL Teritory Manager
                    }
                    else if(Trigger.old[i].PLAS__c != Trigger.new[i].PLAS__c && Trigger.old[i].PLAS__c == exsAG.Team_member_User__c && exsAG.Role__c == 'PL Territory Manager'){
                        agtDelete.put(exsAG.Id, exsAG); //deletes existing agency teams for PL Teritory Manager
                    }
                    else if(Trigger.old[i].CL_Agency_Specialist__c != Trigger.new[i].CL_Agency_Specialist__c && Trigger.old[i].CL_Agency_Specialist__c == exsAG.Team_member_User__c && exsAG.Role__c == 'CL Agency Specialist'){
                        agtDelete.put(exsAG.Id, exsAG); //deletes existing agency teams for CL Agency Specialist
                    }
                    else if(Trigger.old[i].PL_Agency_Specialist__c != Trigger.new[i].PL_Agency_Specialist__c && Trigger.old[i].PL_Agency_Specialist__c == exsAG.Team_member_User__c && exsAG.Role__c == 'PL Agency Specialist'){
                        agtDelete.put(exsAG.Id, exsAG); //deletes existing agency teams for PL Agency Specialist
                    }
                }
                accounts.add(Trigger.new[i]);//Adding filtered i.e which are changed records to a list.
                oldAccounts.add(Trigger.old[i]);
            }
        }
        for(Integer x=0;x<accounts.size();x++){//Generating HRIds with filtered Account records.
            hrIds.add(accounts[x].OwnerId);
            if(accounts[x].PLAS__c != null){
                hrIds.add(accounts[x].PLAS__c);
            }
            if(accounts[x].CL_Agency_Specialist__c != null){
                hrIds.add(accounts[x].CL_Agency_Specialist__c);
            }
            if(accounts[x].PL_Agency_Specialist__c != null){
                hrIds.add(accounts[x].PL_Agency_Specialist__c);
            }
        }
        
        if(agtDelete != null){
            if(agtDelete.size() > 0)delete (agtDelete.values());
        }
        Account_To_AgencyTeam_PLAS.insertAgencyTeam(accounts, hrIds, oldAccounts);
        
    }
}